package cn.qvtu.game.model;

import cn.qvtu.game.util.StaticValue;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Person implements Runnable {
    protected int x;// x坐标
    protected int y;// y坐标
    protected BufferedImage ShowImage;// 图像
    protected Thread thread;// 线程
    protected double moving = 0.0; // 在person目录里循环
    protected int status = 1; // 状态 默认向右站立
    protected int jumpForce = 0; // 跳跃力度
    protected int jumpMoveDirection = 0; // 跳跃方向 (0 垂直 1 左上 2 右上)
    protected BackGround background; // 背景
    private int score = 0; // 得分

//    protected ArrayList<BufferedImage> LeftStandImages;// 左站立图集
//    protected ArrayList<BufferedImage> RightStandImages;// 右站立图集
//    protected ArrayList<BufferedImage> LeftRunImages;// 左跑图集
//    protected ArrayList<BufferedImage> RightRunImages;// 右跑图集
//    protected ArrayList<BufferedImage> LeftJumpImages;// 左跳跃图集
//    protected ArrayList<BufferedImage> RightJumpImages;// 右跳跃图集
//    protected ArrayList<BufferedImage> LeftAttackImages;// 左攻击图集
//    protected ArrayList<BufferedImage> RightAttackImages;// 右攻击图集

    /**
     * 向左站立 -1 向右站立 1
     * 向左跑 -2 向右跑 2
     * 向左跳跃 -3 向右跳跃 3
     * 向左攻击 -4 向右攻击 4
     */
    public Person() {
    }

    public Person(int x, int y,BackGround background) {
        this.x = x;
        this.y = y;
        this.background = background;
        this.thread = new Thread(this);
        thread.start();
    }

//    protected void setImageList(){
//        this.LeftStandImages = (ArrayList<BufferedImage>) StaticValue.LeftPersonImages.subList(0,4);
//        this.RightStandImages = (ArrayList<BufferedImage>) StaticValue.RightPersonImages.subList(0,4);
//        this.LeftRunImages = (ArrayList<BufferedImage>) StaticValue.LeftPersonImages.subList(5,9);
//        this.RightRunImages = (ArrayList<BufferedImage>) StaticValue.RightPersonImages.subList(5,9);
//        this.LeftJumpImages = (ArrayList<BufferedImage>) StaticValue.LeftPersonImages.subList(6,7);
//        this.RightJumpImages = (ArrayList<BufferedImage>) StaticValue.RightPersonImages.subList(6,7);
//        this.LeftAttackImages = (ArrayList<BufferedImage>) StaticValue.LeftPersonImages.subList(9,12);
//        this.RightAttackImages = (ArrayList<BufferedImage>) StaticValue.RightPersonImages.subList(9,12);
//    }
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String toString() {
        return "Person{x = " + x + ", y = " + y + "}";
    }

    public BufferedImage GetShowImage() {
        if (ShowImage == null) {
            ShowImage = StaticValue.RightPersonImages.get(0);
        }
        return ShowImage;
    }

    public void SetShowImage(BufferedImage showImage) {
        ShowImage = showImage;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public BackGround getBackground() {
        return background;
    }

    public void setBackground(BackGround background) {
        this.background = background;
    }

    // 右跑
    public void RunRight() {
        // 浮空时不改变状态
        if (this.status == 3 || this.status == -3) {
            return;
        }
        this.status = 2;
    }

    // 停止右跑
    public void StopRunRight() {
        // 浮空时不改变状态
        if (this.status == 3 || this.status == -3) {
            this.jumpMoveDirection = 0;// 停止跳跃时恢复站立
            return;
        }
        this.status = 1;
    }

    // 左跑
    public void RunLeft() {
        // 浮空时不改变状态
        if (this.status == 3 || this.status == -3) {
            this.jumpMoveDirection = 0;// 停止跳跃时恢复站立
            return;
        }
        this.status = -2;
    }

    // 停止左跑
    public void StopRunLeft() {
        // 浮空时不改变状态
        if (this.status == 3 || this.status == -3) {
            return;
        }
        this.status = -1;
    }

    // 跳跃
    public void Jump() {
        if (this.status != 3 && this.status != -3){
            if (this.status == 2 || this.status == -2){
                this.jumpMoveDirection = this.status>0?1:-1;
            }
            this.status = this.status>0?3:-3;// 设置状态为跳跃状态
            this.jumpForce = 15; // 设置跳跃力度
        }
    }

    // 攻击
    public void Attack() {
        if (this.status != 3 && this.status != -3){
            if (this.status > 0){
                this.status = 4;
            }
            else {
                this.status = -4;
            }
        }
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public void run() {
        // 线程更新人物
        while (true) {
            try {
                switch (this.status) {
                    case 1:
                        // 向右-站立
                        if (this.moving > 4) {// 循环站立的图片
                            this.moving = 0;
                        }
                        this.ShowImage = StaticValue.RightPersonImages.get((int) this.moving);// 更新图像
                        this.moving += 0.2;
                        break;
                    case 2:
                        // 向右-跑
                        if (this.moving < 5 || this.moving > 8) {// 循环奔跑的图片
                            this.moving = 5;
                        }
                        this.ShowImage = StaticValue.RightPersonImages.get((int) this.moving);// 更新图像
                        this.moving += 0.2;
                        break;
                    case 3:
                        // 向右-跳跃
                        this.moving = 6;
                        this.ShowImage = StaticValue.RightPersonImages.get((int) this.moving);// 更新图像
                        break;
                    case 4:
                        // 向右-攻击
                        if (this.moving < 9 || this.moving > 11) {// 循环跳跃的图片
                            this.moving = 9;
                        }
                        this.ShowImage = StaticValue.RightPersonImages.get((int) this.moving);// 更新图像
                        this.moving += 0.5;
                        if (this.moving == 11){
                            this.status = this.status>0?1:-1; // 攻击后恢复站立状态
                        }
                        break;
                    case -1:
                        // 向左-站立
                        if (this.moving > 4) {// 循环站立的图片
                            this.moving = 0;
                        }
                        this.ShowImage = StaticValue.LeftPersonImages.get((int) this.moving);// 更新图像
                        this.moving += 0.2;
                        break;
                    case -2:
                        // 向左-跑
                        if (this.moving < 5 || this.moving > 8) {// 循环奔跑的图片
                            this.moving = 5;
                        }
                        this.ShowImage = StaticValue.LeftPersonImages.get((int) this.moving);// 更新图像
                        this.moving += 0.2;
                        break;
                    case -3:
                        // 向左-跳跃
                        this.moving = 6;
                        this.ShowImage = StaticValue.LeftPersonImages.get((int) this.moving);// 更新图像
                        break;
                    case -4:
                        // 向左-攻击
                        if (this.moving < 9 || this.moving > 11) {// 循环跳跃的图片
                            this.moving = 9;
                        }
                        this.ShowImage = StaticValue.LeftPersonImages.get((int) this.moving);// 更新图像
                        this.moving += 0.5;
                        if (this.moving == 11){
                            this.status = this.status>0?1:-1; // 攻击后恢复站立状态
                        }
                        break;
                    default:
                        break;
                }

                // 人物移动
                moveXY();

                // 边界检测
                if (this.x < -100) {
                    this.x = -100;
                }
                if (this.x > 750) {
                    this.x = 750;
                }

                // 攻击敌人
                if (this.background != null){
                    ArrayList<Enemy> enemies = (ArrayList<Enemy>) this.background.getEnemyList();// 获取敌人列表
                    for (int i = 0; i < enemies.size(); i++) {// 遍历敌人列表 看看哪个敌人在攻击范围
                        Enemy enemy = enemies.get(i);
                        if (this.status == 4 && (this.x +125)>enemy.getX() && (this.x +125)<enemy.getX()+250){
                            enemy.Dead(); // 向右攻击敌人死亡
                            this.score += 100; // 加分
                        }
                        else if (this.status == -4 && (this.x + 125)<(enemy.getX()+400) && (this.x +125)>enemy.getX()+256){
                            enemy.Dead(); // 向左攻击敌人死亡
                            this.score += 100; // 加分
                        }
                    }
                }

                Thread.sleep(50);// 每隔50毫秒更新一次
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // 人物移动
    private void moveXY() {
        switch (this.status) {
            case 1: case -1:
                // 站立
                break;
            case 2:
                // 向右-跑
                this.x += 10;
                break;
            case 3:case -3:
                // 跳跃
                if (this.jumpMoveDirection > 0){
                    this.x += 10;// 向右跳跃
                }
                else if (this.jumpMoveDirection < 0){
                    this.x -= 10;// 向左跳跃
                }
                this.y -= this.jumpForce;
                if (this.y > 305){
                    this.y = 305;// 跳跃高度限制

                    if (this.jumpMoveDirection > 0){
                        this.status = 2; // 跳跃后恢复向右跑状态
                    }
                    else if (this.jumpMoveDirection < 0){
                        this.status = -2; // 跳跃后恢复向左跑状态
                    }
                    else{
                        this.status = this.status>0?1:-1; // 跳跃后恢复站立状态
                    }
                }
                jumpForce--;
                break;
            case 4:case -4:
                // 攻击
                break;
            case -2:
                // 向左-跑
                this.x -= 10;
                break;
            default:
                break;
        }
    }
}
